﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Abstraction for HashMap (Dictionary) to help porting
 * exercises to different languages.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FileSystem.Utility
{
    class XMap<Key, Value>
    {
        private Dictionary<Key, Value> entries;

	    public XMap() {
            entries = new Dictionary<Key, Value>();
	    }
	
        /// <summary>
        /// Indicates whether or not key (not-null) is already in the map.
        /// </summary>
        /// <param name="key">The key to test.</param>
        /// <returns>False is returned if key is null.</returns>
	    public bool containsKey(Key key) {
		    if (null == key) return false;
 		    return entries.ContainsKey(key);
	    }
	
        /// <summary>
        /// Add or Replace a given key (not-null) with a given value (not-null).
        /// </summary>
        /// <param name="key">The key to use.</param>
        /// <param name="value">The value to associate with the key.</param>
	    public void put(Key key, Value value) {
		    if (null == value) return;
		    if (null == key) return;
		    if (containsKey(key)) {
			    entries[key] = value;	
		    } else {
			    entries.Add(key, value);
		    }
	    }
	
        /// <summary>
        /// Gets the value associated with the given key (not-null).
        /// </summary>
        /// <param name="key">The key to use.</param>
        /// <returns>The associated value, or null if key is null, or containsKey() == false.</returns>
	    public Value get(Key key) {
		    if (null == key) return default(Value);
		    if (!containsKey(key)) return default(Value);
		    return entries[key];
	    }
	
        /// <summary>
        /// Returns the keys as an enumerable collection.
        /// </summary>
        /// <returns>The keys</returns>
        public ICollection<Key> getKeys() {
		    return entries.Keys;
	    }

        /// <summary>
        /// Removes and returns the value associated with the given key.
        /// </summary>
        /// <param name="key">The key to use.</param>
        /// <returns>The associated value, or null if key is null, or containsKey() == false.</returns>
        public Value remove(Key key) {
            Value v = get(key);
            entries.Remove(key);
            return v;
        }
    }
}
